var area;
$.post(server_url + "/rest/tokenValidate/getSignature.do", {
	"url" : window.location.href.split('#')[0]
}, function(result) {
	wx.config({
		debug : false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
		appId : appId, // 必填，公众号的唯一标识
		timestamp : result.timestamp, // 必填，生成签名的时间戳
		nonceStr : result.noncestr, // 必填，生成签名的随机串
		signature : result.signature, // 必填，签名，见附录1
		jsApiList : [ 'getLocation', 'scanQRCode' ]
	// 必填，需要使用的JS接口列表，所有JS接口列表见附录2
	});
});
//打开扫一扫
$("#scan").bind('click', function() {
	wx.scanQRCode({
		needResult : 0,
		desc : 'scanQRCode desc',
		success : function(res) {
		}
	});
});
wx.ready(function() {
	wx.checkJsApi({
		jsApiList : [ 'getLocation' ],
		success : function(res) {
			if (res.checkResult.getLocation == false) {
				alert('你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！');
				return;
			}
		}
	});
	wx.getLocation({
		success : function(res) {
			latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
			longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
			var speed = res.speed; // 速度，以米/每秒计
			var accuracy = res.accuracy; // 位置精度
			localStorage.setItem("lon", longitude);
			localStorage.setItem("lat", latitude);
			userid = localStorage.getItem('userid');
			$.ajax({
				type : "get",
				url : server_url
				+ '/rest/teaUserPlace/getUserPlace.do?userId='
				+ userid + '&lon=' + longitude + '&lat=' + latitude
				+ '&ran=' + Math.random(),
				dataType : 'json',
				async : false,
				data : {
					token : token
				},
				success : function(result) {
					if (result.retCode == '0000') {
						if(localStorage.getItem(localStorage.getItem("username"))==1){
							localStorage.setItem(localStorage.getItem("username"),2);
							$("#expressArea").html(
									result.data.split("]")[1].substring(0, 2));
							localStorage.setItem("index-location", result.data
									.split("]")[1].substring(0, 2));// 用户的定位位置保存一下
							
							area = result.data.split("]")[1].substring(0, 2);
							initTeaMountain();
							initButton();
							initBottom();
						}
					} else {
						
					}
				}
			});
		},
		cancel : function(res) {
			alert('用户拒绝授权获取地理位置');
		},
		error : function(res) {
			console.log(res);
		}
	});
});
console.log(localStorage.getItem(localStorage.getItem("username")));


$(function() {
	initUserPlace();
	initSideShowPic();
});
// 初始化用户的位置
function initUserPlace() {
	var index_location = localStorage.getItem("index-location");
	if (index_location) {
		$("#expressArea").html(index_location);
		changeArea(index_location);
		initButton();
		initBottom();
		return;
	}

}
// 初始化用户轮播图
function initSideShowPic() {
	$.ajax({
		type : "get",
		url : server_url
				+ '/rest/TeaSideshowPicController/getTeaSideshowPicList.do?ran='
				+ Math.random(),
		dataType : 'json',
		async : false,
		data : {
			token : token
		},
		success : function(result) {
			if (result.retCode == '0000') {
				var sideShow = $(".swiper-container1 .swiper-wrapper");
				var pics = result.data;
				var str = "";
				$.each(pics, function(i, n) {
//					str += '<div class="swiper-slide"><img src="'
//							+ n.picPath + '" /></div>';
					
					str+='<div class="swiper-slide sideShowPic" type="'+n.type+'" productid="'+n.productId+'"><img src="'+n.picPath+'"/></div>';
				})
				sideShow.html('');
				sideShow.html(str);
				var mySwiper = new Swiper('.swiper-container1', {
					direction : 'horizontal',
					loop : true,
					// 如果需要分页器
					pagination : '.swiper-pagination'
				});
			} else {

			}
			
		}
	});
	//初始化茶山交易量
	$.ajax({
		type : "get",
		url : server_url + 'rest/tea/product/getTeaCount.do?ran='
				+ Math.random(),
		dataType : 'json',
		async : false,
		data : {
			token : token
		},
		success : function(result) {
			if (result.retCode == '0000') {
				if(result.data.teaCount==0){
					result.data.teaCount = 900;
				}
				if(result.data.teaSuccessCount==0){
					result.data.teaSuccessCount = 900;
				}
				$("#teaCount").html(result.data.teaCount);
				$("#teaSuccessCount").html(result.data.teaSuccessCount);
			} else {

			}
		}
	});
}
// 初始化茶山数据
function initTeaMountain() {
	//获取热门推荐
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getTeaHotList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			area:area
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var productList = result.data;
				if(productList.length==0){
					if(area!=''){
						area = '';
						initTeaMountain();
					}
				}
				var str = '';
				$.each(productList,function(i,n){
					str+='<div class="swiper-slide hotPro" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'"/><div class="img-det clear"><span class="fl">'+n.teaTitle+'</span><span class="fr">'+n.legalName+":"+n.teaPhone+'</span></div></div>';
				});
				$('#teaHost').html(str);
				var mySwiper1 = new Swiper('.swiper-container2', {
					direction : 'horizontal',
					loop : true
				});
			}else{
				showErrorMsg(result.data);
			}
		}
	});
	//获取列表
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getTeaGardenList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data:{
			token : token,
			index:"index",
			area:area
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				var productList = result.data;
				if(productList.length==0){
					if(area!=''){
						area = '';
						initTeaMountain();
					}
				}
				var str = '';
				$.each(productList,function(i,n){
					var gandenTypeStr = '';
					if(n.gandenType==0){
						gandenTypeStr = "国家生态林";
					}else if(n.gandenType==1){
						gandenTypeStr = "成型茶园";
					}else if(n.gandenType==2){
						gandenTypeStr = "荒山茶林";
					}/*<span class="fl color02">转让</span>*/
					var rengStr = '';
					if(n.rentType==0){
						//整租
						rentStr = '<span class="fl color02">整租</span>'
					}else{
						//分租
						rentStr = '<span class="fl color03">分租</span>'
					}
					str +=' <div class="list-item clear" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" class="fl"><div class="item-r fl"><p class="item-tit">'+n.teaTitle+'</p><div class="item-det clear"><span class="fl color01">'+gandenTypeStr+'</span>'+rentStr+'<span class="fl color03">已认证</span></div><div class="item-time clear"><p class="fl">'+n.teaArea+'亩</p><span class="fr">'+n.limitTime+'年</span></div><div class="num-box clear"><div class="fl">￥<span>'+n.teaPrice+'</span>元/亩·年</div><p class="fr">'+n.teaLocation+'</p></div></div></div>';
				});
				$('.index-list').html(str);
			}else{
				showErrorMsg(result.data);
			}
		}
	});
}
// 页面事件绑定
function initButton() {
	// 跳转到茶旅联盟
	$("#tea-team").bind('click', function() {
		window.location = server_url + "teaTeam.do";
		//window.location = server_url + "teaClub.do";
	});
	
	// 跳转到茶人茶艺
	$("#skill-list").bind('click', function() {
		window.location = server_url + "skillList.do";
	});
	
	$("#calculator").bind('click', function() {
		window.location = server_url + "calculator.do";
	});
	
	
	
	// 跳转到茶叶频道
	$("#tea-pro-list").bind('click', function() {
		var param = {};
		param.type = '3';
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "proList.do";
	});
	//跳转到茶产品频道
	$("#pro-list").bind('click', function() {
		var param = {};
		param.type = '4';
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location = server_url + "proList.do";
	});
	//搜索事件
	$('.search-con').on('click',function(){
		localStorage.setItem('oldPath',"buyer/product/index.do");
		window.location = server_url + "searchResult.do";
	});
	
	//热门推荐
	$('body').on('click','.hotPro',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	
	//轮播图
	$('body').on('click','.sideShowPic',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
	
	$('body').on('click','.list-item',function(){
		var type = $(this).attr('type');
		var productId = $(this).attr('productid');
		var param = {};
		jumpDetail(type,param,productId);
	});
}
// 绑定下列表切换
function initBottom() {
	// 跳转到茶山市场
	$("#tea-market").bind('click', function() {
		window.location = server_url + "teaMarket.do";
	});
	//跳转到我的
	$("#tea-my").bind('click', function() {
		window.location = server_url + "teaMy.do";
	});
	//跳转到头条咨询
	$("#inf").bind('click', function() {
		window.location = server_url + "inf.do";
	});
	//跳转到发布
	$(".demand-btn").bind('click', function() {
		window.location = server_url + "release.do";
	});
}
//改变地区，重新根据地区进行过滤
function changeArea(areaP){
	area = areaP;
	localStorage.setItem("index-location",areaP);
	initTeaMountain();
}